package com.shxc.smarttrade.old.myview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.utils.BaseUtils;

/**
 * Created by jcdev1 on 2017/9/14.
 */

public class CommonDialog extends Dialog implements View.OnClickListener {
    private Context mContext;

    private TextView tvTitle;
    private TextView tvMessage;
    private TextView btnYes;
    private TextView btnNo;
    private String title = "";
    private String message = "";
    private String no = "";
    private String yes = "";
    private Runnable noRun;
    private Runnable yesRun;

    public CommonDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public CommonDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    protected CommonDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.normal_dialog_style);
        initView();
    }

    private void initView() {

        int width = BaseUtils.getScreenWidth(mContext);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width;
        lp.alpha = 1;
        lp.dimAmount = 0.4f;
        window.setAttributes(lp);

        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvMessage = (TextView) findViewById(R.id.tv_message);
        btnYes = (TextView) findViewById(R.id.btn_sure);
        btnNo = (TextView) findViewById(R.id.btn_cancles);
        btnYes.setOnClickListener(this);
        btnNo.setOnClickListener(this);
        btnYes.setText(yes);
        btnNo.setText(no);
        tvTitle.setText(title);
        tvMessage.setText(message);
    }

    public void setBaseText(String title, String message, String no, String yes) {
        this.title = title;
        this.message = message;
        this.no = no;
        this.yes = yes;
    }

    public void setYesCLickRunAble(Runnable yesRunable) {
        yesRun = yesRunable;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancles:
                if (noRun != null)
                    noRun.run();
                this.dismiss();
                break;
            case R.id.btn_sure:
                if (yesRun != null)
                    yesRun.run();
                this.dismiss();
                break;
        }

    }

}
